package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

public class GetRequestNormal extends AxdrType {
    private byte[] dataCode = null;
    private PIID piid = null;
    private OAD oad = null;

    public GetRequestNormal() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid, OAD oad) {
        this.piid = piid;
        this.oad = oad;
    }

    public void setOAD(OAD oad) {
        this.oad = oad;
    }

    public void setPIID(PIID piid) {
        this.piid = piid;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);
        this.oad = new OAD();
        codeLength += this.oad.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.oad != null && this.piid != null) {
                codeLength += this.oad.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new Exception("oad or piid is null");
            }
        }
        logEncodeMsg("GetRequestNormal", output);
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.piid != null && this.oad != null) {
            return "sequence: {" + "piid: " + this.piid + ", oad: " + this.oad + "}";
        } else {
            return "piid || oad is null";
        }
    }
}



